#include "btform.h"
#include "ui_btform.h"


BtForm::BtForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::BtForm)
{
    ui->setupUi(this);

    ui->lineBox->hide();

    // 设置line1的动画效果
    line1Animation = new QPropertyAnimation(ui->line1, "geometry", this);
    line1Animation->setDuration(1500);
    line1Animation->setKeyValueAt(0, QRect(0, 15, 2, 0));
    line1Animation->setKeyValueAt(0.5, QRect(0, 0, 2, 15));
    line1Animation->setKeyValueAt(1, QRect(0, 15, 2, 0));
    line1Animation->setLoopCount(-1);
    line1Animation->start();

    // 设置line2的动画效果
    line2Animation = new QPropertyAnimation(ui->line2, "geometry", this);
    line2Animation->setDuration(1600);
    line2Animation->setKeyValueAt(0, QRect(8, 15, 2, 0));
    line2Animation->setKeyValueAt(0.5, QRect(8, 0, 2, 15));
    line2Animation->setKeyValueAt(1, QRect(8, 15, 2, 0));
    line2Animation->setLoopCount(-1);
    line2Animation->start();

    // 设置line1的动画效果
    line3Animation = new QPropertyAnimation(ui->line3, "geometry", this);
    line3Animation->setDuration(1700);
    line3Animation->setKeyValueAt(0, QRect(16, 15, 2, 0));
    line3Animation->setKeyValueAt(0.5, QRect(16, 0, 2, 15));
    line3Animation->setKeyValueAt(1, QRect(16, 15, 2, 0));
    line3Animation->setLoopCount(-1);
    line3Animation->start();

    // 设置line1的动画效果
    line4Animation = new QPropertyAnimation(ui->line4, "geometry", this);
    line4Animation->setDuration(1800);
    line4Animation->setKeyValueAt(0, QRect(24, 15, 2, 0));
    line4Animation->setKeyValueAt(0.5, QRect(24, 0, 2, 15));
    line4Animation->setKeyValueAt(1, QRect(24, 15, 2, 0));
    line4Animation->setLoopCount(-1);
    line4Animation->start();

}

BtForm::~BtForm()
{
    delete ui;
}

void BtForm::setIconAndText(const QString &btIcon, const QString &btText, int pageId)
{
    // 设置按钮的图标
    ui->btIcon->setPixmap(QPixmap(btIcon));

    // 设置按钮文本
    ui->btText->setText(btText);

    // 将按钮和qtmusic中的Page页面关联起来
    this->pageId = pageId;
}

void BtForm::setBg()
{
    QString style = "#btIcon{background-color:rgb(30,206,154);}";
    style += "#btText{background-color:rgb(30,206,154);}";
    style += "#lineBox{background-color:rgb(30,206,154);}";
    style += "#btStyle{background-color:rgb(30,206,154);}";

    ui->btStyle->setStyleSheet(style);
}

void BtForm::clearBg()
{
    ui->btStyle->setStyleSheet("#btText:hover{background-color: #d8d8d8;}");
}

void BtForm::showAnimation()
{
    ui->lineBox->show();
}

void BtForm::hideAnimation()
{
    ui->lineBox->hide();
}

int BtForm::PageId()
{
    return pageId;
}

void BtForm::mousePressEvent(QMouseEvent *event)
{
    (void)event;
    // 1、当按钮按下之后，需要修改其背景颜色
    QString style = "#btIcon{background-color:rgb(30,206,154);}";
    style += "#btText{background-color:rgb(30,206,154);}";
    style += "#lineBox{background-color:rgb(30,206,154);}";
    style += "#btStyle{background-color:rgb(30,206,154);}";

    ui->btStyle->setStyleSheet(style);
    // 2、当按钮按下之后，需要切换bodyRight中的Page页面
    // 什么时机切换？
    // 由谁完成页面切换
    // 按钮和Page页面的对应关系
    emit btClick(this->pageId);
}
